package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DeptZhuanjia;
import com.ruoyi.system.service.IDeptZhuanjiaService;
import com.ruoyi.system.service.IDeptidOlddeptidService;
import com.ruoyi.system.service.IZhuanjiaBiandongDetailService;
import com.ruoyi.system.tools.BooleanMessage;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单位专家关联Controller
 * 
 * @author ruoyi
 * @date 2022-12-08
 */
@Controller
@RequestMapping("/system/deptzhuanjia")
public class DeptZhuanjiaController extends BaseController
{
    private String prefix = "system/deptzhuanjia";

    @Autowired
    private IDeptZhuanjiaService deptZhuanjiaService;

    @Autowired
    private IZhuanjiaBiandongDetailService zhuanjiaBiandongDetailService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    @RequiresPermissions("system:deptzhuanjia:view")
    @GetMapping()
    public String deptzhuanjia()
    {
        return prefix + "/deptzhuanjia";
    }

    /**
     * 查询单位专家关联列表
     */
    @RequiresPermissions("system:deptzhuanjia:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DeptZhuanjia deptZhuanjia)
    {
        startPage();
        List<DeptZhuanjia> list = deptZhuanjiaService.selectDeptZhuanjiaList(deptZhuanjia);
        return getDataTable(list);
    }

    /**
     * 导出单位专家关联列表
     */
    @RequiresPermissions("system:deptzhuanjia:export")
    @Log(title = "单位专家关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeptZhuanjia deptZhuanjia)
    {
        List<DeptZhuanjia> list = deptZhuanjiaService.selectDeptZhuanjiaList(deptZhuanjia);
        ExcelUtil<DeptZhuanjia> util = new ExcelUtil<DeptZhuanjia>(DeptZhuanjia.class);
        return util.exportExcel(list, "单位专家关联数据");
    }

    /**
     * 新增单位专家关联
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存单位专家关联
     */
    @RequiresPermissions("system:deptzhuanjia:add")
    @Log(title = "单位专家关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DeptZhuanjia deptZhuanjia)
    {
        return toAjax(deptZhuanjiaService.insertDeptZhuanjia(deptZhuanjia));
    }

    /**
     * 修改单位专家关联
     */
    @RequiresPermissions("system:deptzhuanjia:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        DeptZhuanjia deptZhuanjia = deptZhuanjiaService.selectDeptZhuanjiaById(id);
        mmap.put("deptZhuanjia", deptZhuanjia);
        return prefix + "/edit";
    }

    /**
     * 修改保存单位专家关联
     */
    @RequiresPermissions("system:deptzhuanjia:edit")
    @Log(title = "单位专家关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DeptZhuanjia deptZhuanjia)
    {
        return toAjax(deptZhuanjiaService.updateDeptZhuanjia(deptZhuanjia));
    }

    /**
     * 删除单位专家关联
     */
    @RequiresPermissions("system:deptzhuanjia:remove")
    @Log(title = "单位专家关联", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(deptZhuanjiaService.deleteDeptZhuanjiaByIds(ids));
    }

    /*
    * 根据单位ID查询专家
    * */
    @PostMapping("/find_by_deptid")
    @ResponseBody
    public List<DeptZhuanjia> list(@RequestParam("deptid") String ppDeptId,@RequestParam("zhuanjianame")String ppZhuanjiaName)
    {
        DeptZhuanjia mmDeptZhuanjia = new DeptZhuanjia();
        mmDeptZhuanjia.setDeptid(deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId));
        mmDeptZhuanjia.setXingming("%"+ppZhuanjiaName+"%");
        mmDeptZhuanjia.setIsvalid((long)1);
        List<DeptZhuanjia> list = deptZhuanjiaService.selectDeptZhuanjiaList(mmDeptZhuanjia);

        return list;
    }

    /*
     * 根据单位ID查询专家
     * */
    @PostMapping("/find_by_Deptid")
    @ResponseBody
    public List<DeptZhuanjia> DeptZhuanjialist(@RequestParam("deptid") String ppDeptId,@RequestParam("zhuanjianame")String ppZhuanjiaName)
    {
        DeptZhuanjia mmDeptZhuanjia = new DeptZhuanjia();
        mmDeptZhuanjia.setDeptid(deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId));
        mmDeptZhuanjia.setXingming("%"+ppZhuanjiaName+"%");
        mmDeptZhuanjia.setIsvalid((long)1);
        List<DeptZhuanjia> list = deptZhuanjiaService.selectDeptZhuanjiaListByDeptid(mmDeptZhuanjia);
        return list;
    }

    @PostMapping("/zhuanyi")
    @ResponseBody
    public JsonResult<?> zhuanyi(@RequestParam("zhuanjiaids")String ppZhuanjiaIds,@RequestParam("deptid")String ppDeptId){
        try{
            BooleanMessage bm = deptZhuanjiaService.zhuanyi(ppZhuanjiaIds,ppDeptId);
            return bm.isOk()?JsonResult.getSuccessResult("转移成功"):JsonResult.getErrorResult(bm.getMessage().toString());
        }catch (Exception e){
            logger.error("deptzhuanjia -> zhuanyi: "+e.getMessage());
            return JsonResult.getErrorResult("转移专家失败");
        }
    }

    @GetMapping("/zhuanjiazhuanyi")
    public String zhuanjiazhuanyi(@RequestParam("deptid")String ppOldDeptId, ModelMap mmap)
    {
        String mmDeptId = deptidOlddeptidService.OldDeptIdToDeptId(ppOldDeptId);
        mmap.put("oldid", ppOldDeptId);
        mmap.put("id", mmDeptId);
        return prefix + "/zhuanjiazhuanyi";
    }

}
